package xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.presenter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;
import java.util.Map;

import xixirupan.tmgp.tencent.com.vmeim.R;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model.LiveListBaen;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model.Nav1BannersBaen;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model.ProductsBaen;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model.TopBannersBaen;

/**
 * Created by lenovo on 2016/10/19.
 */

public class IndexRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int    TYPE1 = 1;
    public static final int    TYPE2 = 2;
    public static final int    TYPE3 = 3;
    public static final int    TYPE4 = 4;
    public static final int    TYPE5 = 5;
    public static final int    TYPE6= 6;
   private List<Object> list;
    private Context context;
    private LayoutInflater inflater;
    boolean flag=false;
  // private List<Map<String,Object>> lists;

   public  IndexRecyclerAdapter(List<Object> list ,Context context,LayoutInflater inflater,boolean flag)
   {
       this.list  =list  ;
       this.context =context;
       this.inflater =inflater;
       this.flag =flag;

   }

    @Override
    public int getItemViewType(int position) {
        if (flag)
        {
            if (list.get(position) instanceof  String)
            {
                return 5;
            }else  if (list.get(position) instanceof  List )
            {
                return 6;
            }
        }
      else{
            if (position ==0)
            {
                return 1;

            }else if (position ==1)
            {
                return 2;
            }
            else if (position ==2)
            {
                return 3;
            }else if (position ==3)
            {
                return 4;
            }else  if (list.get(position) instanceof  String)
            {
                return 5;
            }else  if (list.get(position) instanceof  List )
            {
                return 6;
            }
        }
           return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         View view;
        if (viewType == TYPE1)
        {
             view = inflater.inflate(R.layout.item1_shop_layout, parent, false);
            ViewHolderShop1 shop1 =new ViewHolderShop1(view);
            return shop1;

        }else if (viewType ==TYPE2)
        { view = inflater.inflate(R.layout.item2_shop_layout, parent, false);
            ViewHolderShop2 shop2 =new ViewHolderShop2(view);
            return shop2;

        }else if (viewType ==TYPE3)
        { view = inflater.inflate(R.layout.item3_shop_layout, parent, false);
            ViewHolderShop3 shop3 =new ViewHolderShop3(view);
            return shop3;

        }else  if (viewType ==TYPE4)
        { view = inflater.inflate(R.layout.item4_shop_layout, parent, false);
            ViewHolderShop4 shop4 =new ViewHolderShop4(view);
            return shop4;

        }else  if (viewType ==TYPE5)
        {
            view =inflater.inflate(R.layout.inner_item5index_layout,parent,false);
            ViewHolderShop5 shop5 =new ViewHolderShop5(view);
            return shop5;
        }else if (viewType == TYPE6)
        {
            view =inflater.inflate(R.layout.inner_item5_index_layout,parent,false);
            ViewHolderShop6 shop6 =new ViewHolderShop6(view);
            return shop6;

        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
              if (holder instanceof ViewHolderShop1)
              {
                  List<TopBannersBaen> topBanners = (List<TopBannersBaen>) list.get(position);
                  ViewPagerAdapter adapter =new ViewPagerAdapter(context,topBanners);
                  ((ViewHolderShop1) holder).viewPager.setAdapter(adapter);
              }else  if (holder instanceof ViewHolderShop2)
              {
                 //
                  List<Nav1BannersBaen>  navbean = (List<Nav1BannersBaen>) list.get(position);
                  LinearLayoutManager manager =new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
                  ((ViewHolderShop2) holder).recyclerView.setLayoutManager(manager);
                   RecyIndexItem2Adapter adapter =new RecyIndexItem2Adapter(navbean,context,inflater);
                  ((ViewHolderShop2) holder).recyclerView.setAdapter(adapter);
              }else if (holder instanceof ViewHolderShop3)
              {
               List<Map<String,String>> lists = (List<Map<String, String>>) list.get(position);


                   ((ViewHolderShop3) holder).recyclerView.setLayoutManager(new GridLayoutManager(context,2));
                   Item3RecyAdapter adapter =new Item3RecyAdapter(lists,inflater,context);
                  ((ViewHolderShop3) holder).recyclerView.setAdapter(adapter);
              }else if (holder instanceof ViewHolderShop4)
              {
                  List<LiveListBaen> liveList = (List<LiveListBaen>) list.get(position);

                  GridLayoutManager manager =new GridLayoutManager(context,1,LinearLayoutManager.HORIZONTAL,false);
                   ((ViewHolderShop4) holder).recyclerView.setLayoutManager(manager);
                 Item4IndexAdapter adapter =new Item4IndexAdapter(liveList,context,inflater);
                   ((ViewHolderShop4) holder).recyclerView.setAdapter(adapter);

              }else  if (holder instanceof  ViewHolderShop5)
              {

                 String url = (String) list.get(position);
                  Log.i("TAG///////////","--------->"+url);
                  Glide.with(context).load(url).into(((ViewHolderShop5) holder).imageView);
              }else if (holder instanceof ViewHolderShop6)
              {

                  List<ProductsBaen> products = (List<ProductsBaen>) list.get(position);
                  ((ViewHolderShop6) holder).recyclerView.setLayoutManager(
                          new GridLayoutManager(context,1, LinearLayoutManager.HORIZONTAL,false));
                  Item5RecyAdapter adapter =new Item5RecyAdapter(products,context,inflater);
                  ((ViewHolderShop6) holder).recyclerView.setAdapter(adapter);
              }
    }

    class ViewHolderShop1 extends RecyclerView.ViewHolder
    {
         ViewPager viewPager;
        public ViewHolderShop1(View itemView) {
            super(itemView);
            viewPager = (ViewPager) itemView.findViewById(R.id.item1_viewpager_shop);
        }
    }
    class ViewHolderShop2 extends RecyclerView.ViewHolder
    {
        RecyclerView recyclerView;

        public ViewHolderShop2(View itemView) {
            super(itemView);
           recyclerView = (RecyclerView) itemView.findViewById(R.id.item2_recyclerview1_shop);


        }
    }class ViewHolderShop3 extends RecyclerView.ViewHolder
    {
        RecyclerView recyclerView;

        public ViewHolderShop3(View itemView) {
            super(itemView);
           recyclerView = (RecyclerView) itemView.findViewById(R.id.item3_gridview_shop);
        }
    }
    class ViewHolderShop4 extends RecyclerView.ViewHolder
    {

        RecyclerView recyclerView;

        public ViewHolderShop4(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.item4_recyclerview_shop);
        }
    }
    class ViewHolderShop5 extends RecyclerView.ViewHolder
    {
        ImageView imageView;

        public ViewHolderShop5(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.inner_recyc_item5);
        }

    }
    class   ViewHolderShop6 extends RecyclerView.ViewHolder
    {
        RecyclerView recyclerView;

        public  ViewHolderShop6(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.inner_item5_recycler);
        }
    }

    @Override
    public int getItemCount() {

        return list.size();
    }
    //加载刷新
    public void reloadListView(List<Object> _list, boolean isClear) {
        if (isClear) {
            list.clear();
        }
        list.addAll(_list);
        //适配器的刷新方法
        notifyDataSetChanged();
    }


}
